﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;

namespace WebSite
{
    public class re
    {
        // Methods
        public static string maxid(string str, string tables, string mid)
        {
            string sql = "Select " + mid + " From " + tables + " order by " + mid + " desc";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                str = (Convert.ToInt64(table.Rows[0][0].ToString()) + 1L) + "";
            }
            else
            {
                str = "1";
            }
            if (str.Trim().ToString() == "")
            {
                str = "1";
            }
            return str.Trim();
        }

        public static string ReadCookie(string CookieName)
        {
            if (HttpContext.Current.Request.Cookies[CookieName] == null)
            {
                return null;
            }
            return HttpContext.Current.Request.Cookies[CookieName].Value;
        }

        public static string replace(string str)
        {
            str = str.Replace("'", "''");
            str = str.Replace("\"", "&quot;");
            str = str.Replace("<", "&lt;");
            str = str.Replace(">", "&gt;");
            str = str.Replace("\n", "<br/>");
            str = str.Replace("\r\n", "<br/>");
            str = str.Replace("select", "");
            str = str.Replace("insert", "");
            str = str.Replace("update", "");
            str = str.Replace("delete", "");
            str = str.Replace("create", "");
            str = str.Replace("drop", "");
            str = str.Replace("delcare", "");
            if (str.Trim().ToString() == "")
            {
                str = "";
            }
            return str.Trim();
        }

        public static string returnclass(string str, string parentid)
        {
            string sql = "Select * From DB_Class Where parentid =" + parentid + " and system='false' Order By classorder";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    if (row["Amount"].ToString() != "0")
                    {
                        str = str + "," + row["classid"].ToString();
                        str = str + "," + returnclass(str, row["classid"].ToString());
                    }
                    else
                    {
                        str = str + "," + row["classid"].ToString();
                    }
                }
            }
            else
            {
                str = str + "," + parentid.ToString();
            }
            if (str.Trim().ToString() == "")
            {
                str = "";
            }
            return str.Trim();
        }



        public static string returntype(string str, string parentid, string languages)
        {
            string str2;
            str = parentid;
            if (parentid.ToString() == "0")
            {
                str2 = "Select * From DB_Productclass where languages=" + languages + " and visible=1 Order By classorder";
            }
            else
            {
                str2 = "Select * From DB_Productclass Where languages=" + languages + " and parentid =" + parentid + "  and visible=1 Order By classorder";
            }
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(str2).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    str = str + "," + row["classid"].ToString();
                    if (row["Amount"].ToString() != "0")
                    {
                        str = str + "," + returntype(str, row["classid"].ToString(), languages);
                    }
                }
            }
            else
            {
                str = str + "," + parentid.ToString();
            }
            if (str.Trim().ToString() == "")
            {
                str = parentid;
            }
            return str.Trim();
        }

        public static string toptype(string str)
        {
            string str3 = "{          style=submenuStyle;         overflow='scroll';     ";
            string str4 = "0";
            string sql = "select * from DB_Productclass where visible=1 and parentid=" + str.ToString() + " and languages=1  order by classorder";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    if (row["amount"].ToString() == "0")
                    {
                        string str6 = str3;
                        str3 = str6 + "aI('text=" + row["classname"].ToString() + ";url=products_category.aspx?categoryid=" + str.ToString() + "&typeid=" + row["classid"].ToString() + ";'); ";
                    }
                    else
                    {
                        string str7 = str3;
                        str3 = str7 + "aI('text=" + row["classname"].ToString() + ";url=products_category.aspx?categoryid=" + str.ToString() + "&typeid=" + row["classid"].ToString() + ";showmenu=subMenu_" + row["classid"].ToString() + ";'); ";
                        str4 = str4 + "," + row["classid"].ToString();
                    }
                }
            }
            str3 = str3 + "}";
            if (str4.ToString() != "0")
            {
                string[] strArray = str4.ToString().Split(new char[] { ',' });
                for (int i = 0; i <= (strArray.Length - 1); i++)
                {
                    if (strArray[i].ToString() != "0")
                    {
                        str3 = str3 + "with(main=new menuname('subMenu_" + strArray[i].ToString() + "'))     {          style=submenuStyle;         overflow='scroll';        ";
                        string str5 = "select * from DB_Productclass where visible=1 and parentid=" + strArray[i].ToString() + " and languages=1  order by classorder";
                        DataTable table2 = new DataTable();
                        table2 = SQLAccess.GreatDs(str5).Tables[0];
                        if (table2.Rows.Count > 0)
                        {
                            foreach (DataRow row2 in table2.Rows)
                            {
                                string str8 = str3;
                                str3 = str8 + "aI('text=" + row2["classname"].ToString() + ";url=products_category.aspx?categoryid=" + str.ToString() + "&typeid=" + row2["classid"].ToString() + ";');         ";
                            }
                        }
                        str3 = str3 + "}";
                    }
                }
            }
            return str3.ToString();
        }

        public static string toptype1(string str)
        {
            string str3 = "{          style=submenuStyle;         overflow='scroll';     ";
            string str4 = "0";
            string sql = "select * from DB_Productclass where visible=1 and parentid=" + str.ToString() + " and languages=3  order by classorder";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    if (row["amount"].ToString() == "0")
                    {
                        string str6 = str3;
                        str3 = str6 + "aI('text=" + row["classname"].ToString() + ";url=products_category.aspx?categoryid=" + str.ToString() + "&typeid=" + row["classid"].ToString() + ";'); ";
                    }
                    else
                    {
                        string str7 = str3;
                        str3 = str7 + "aI('text=" + row["classname"].ToString() + ";url=products_category.aspx?categoryid=" + str.ToString() + "&typeid=" + row["classid"].ToString() + ";showmenu=subMenu_" + row["classid"].ToString() + ";'); ";
                        str4 = str4 + "," + row["classid"].ToString();
                    }
                }
            }
            str3 = str3 + "}";
            if (str4.ToString() != "0")
            {
                string[] strArray = str4.ToString().Split(new char[] { ',' });
                for (int i = 0; i <= (strArray.Length - 1); i++)
                {
                    if (strArray[i].ToString() != "0")
                    {
                        str3 = str3 + "with(main=new menuname('subMenu_" + strArray[i].ToString() + "'))     {          style=submenuStyle;         overflow='scroll';        ";
                        string str5 = "select * from DB_Productclass where visible=1 and parentid=" + strArray[i].ToString() + " and languages=3  order by classorder";
                        DataTable table2 = new DataTable();
                        table2 = SQLAccess.GreatDs(str5).Tables[0];
                        if (table2.Rows.Count > 0)
                        {
                            foreach (DataRow row2 in table2.Rows)
                            {
                                string str8 = str3;
                                str3 = str8 + "aI('text=" + row2["classname"].ToString() + ";url=products_category.aspx?categoryid=" + str.ToString() + "&typeid=" + row2["classid"].ToString() + ";');         ";
                            }
                        }
                        str3 = str3 + "}";
                    }
                }
            }
            return str3.ToString();
        }

        public static string webinfo(string str, string str2, string languages)
        {
            string sql = "select top 1 classname from DB_Productclass where classid=" + str2 + " and languages=" + languages + "";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                str = table.Rows[0]["classname"].ToString();
            }
            if (str.Trim().ToString() == "")
            {
                str = "";
            }
            return str.Trim();
        }

        public static string webinfo1(string str, string str2, string languages)
        {
            string sql = "select top 1 * from DB_Productclass where classid=" + str2 + " and languages=" + languages + "";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                str = table.Rows[0]["parentid"].ToString();
            }
            else
            {
                str = str2;
            }
            if (str.Trim().ToString() == "")
            {
                str = "0";
            }
            return str.Trim();
        }

        public static string webinfo2(string str, string str2, string languages)
        {
            string sql = "select top 1 * from DB_Productclass where classid=" + str2 + " and languages=" + languages + "";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["parentid"].ToString() != "0")
                {
                    str = webinfo2("", table.Rows[0]["parentid"].ToString(), "1");
                }
                else
                {
                    str = str2;
                }
            }
            if (str.Trim().ToString() == "")
            {
                str = "0";
            }
            return str.Trim();
        }

        public static bool WriteCookie(string CookieName, string CookieValue, int ExpiresDate)
        {
            HttpCookie cookie = new HttpCookie(CookieName)
            {
                Value = CookieValue,
                Expires = DateTime.Now.AddDays((double)ExpiresDate)
            };
            HttpContext.Current.Response.Cookies.Add(cookie);
            return false;
        }

    }
}